﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StoreClientModuleEx;
using UnityCore;

namespace MBBStoreClient
{
    public partial class frmProductSearch : Form
    {
        StockManager sm = new StockManager();
        Threader t = new Threader();
        string lastSearch = "";
        int cPage = 0;
        int cRes = 50;
        public frmProductSearch()
        {
            InitializeComponent();

            btnClose.Click += new EventHandler(btnClose_Click);
            panel2.EnabledChanged += new EventHandler(panel2_EnabledChanged);

            sm.ProcessCompleted += new CoreHandler(sm_ProcessCompleted);
            sm.ProcessError += new CoreHandler(sm_ProcessError);
            btnSearch.Click += new EventHandler(SearchProduct);
            btnPrev.Click += new EventHandler(btnPrev_Click);
            btnNext.Click += new EventHandler(btnNext_Click);
            txtSearch.KeyPress += new KeyPressEventHandler(txtSearch_KeyPress);
            btnCopy.Click += new EventHandler(btnCopy_Click);

        }

        void btnCopy_Click(object sender, EventArgs e)
        {
            if(grdResult.SelectedRows.Count == 1) Clipboard.SetText(grdResult.SelectedRows[0].Cells[0].Value.ToString());
        }

      
        void txtSearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                SearchProduct(sender, e);
            }
        }

        void btnNext_Click(object sender, EventArgs e)
        {
            ControlState(false);
            cPage++;
            GenericSearchType gst = new GenericSearchType() { Keyword = lastSearch, PageNum = cPage, Qty = cRes };
            sm.SearchProduct(gst);
        }

        void btnPrev_Click(object sender, EventArgs e)
        {
            ControlState(false);
            cPage--;
            GenericSearchType gst = new GenericSearchType() { Keyword = lastSearch, PageNum = cPage, Qty = cRes };
            sm.SearchProduct(gst);
        }

        void SearchProduct(object sender, EventArgs e)
        {
            ControlState(false);
            lastSearch = txtSearch.Text;
            cPage = 1;
            GenericSearchType gst = new GenericSearchType(){Keyword = lastSearch, PageNum = cPage, Qty = cRes};
            sm.SearchProduct(gst);
        }

        void sm_ProcessError(string pcode, string msg)
        {
            t.SafeCall(this, delegate
            {
                MessageBox.Show(msg, pcode, MessageBoxButtons.OK, MessageBoxIcon.Error);
                ControlState(true);
            });
        }
        void sm_ProcessCompleted(string pcode, string msg)
        {
            t.SafeCall(this, delegate
            {
                switch (pcode)
                {
                    case "SearchProduct":
                        grdResult.DataSource = sm.Products;
                        grdResult.Columns[0].HeaderText = "Barcode";
                        grdResult.Columns[1].HeaderText = "Description";
                        grdResult.Columns[2].HeaderText = "Supplier";
                        grdResult.Columns[3].HeaderText = "Content";
                        grdResult.Columns[4].HeaderText = "Unit";

                        grdResult.Columns[0].Width = (grdResult.Width * 25) / 100;
                        grdResult.Columns[1].Width = (grdResult.Width * 60) / 100;
                        lblSearchResult.Text = "Searh result : " + sm.Products.Rows.Count;
                        lblPageNum.Text = cPage.ToString();
                        if (sm.Products.Rows.Count == cRes)
                        {
                            if (cPage != 1)
                            {
                                btnNext.Enabled = true;
                                btnPrev.Enabled = true;
                            }
                            else
                            {
                                btnNext.Enabled = true;
                                btnPrev.Enabled = false;
                            }
                        }
                        else
                        {
                            if (cPage != 1)
                            {
                                btnNext.Enabled = false;
                                btnPrev.Enabled = true;
                            }
                            else
                            {
                                btnNext.Enabled = false;
                                btnPrev.Enabled = false;
                            }
                        }
                        ControlState(true);
                        break;
                }
            });
        }
        void ControlState(bool state)
        {
            txtSearch.Enabled = state;
            grdResult.Enabled = state;
            btnSearch.Enabled = state;
            pnlNav.Enabled = state;
            if (!state)
            {
                pnlMsg.Visible = true;
            }
            else
            {
                pnlMsg.Visible = false;
            }
        }



        void panel2_EnabledChanged(object sender, EventArgs e)
        {
            if (!panel2.Enabled)
            {
                panel2.BackColor = Color.MidnightBlue;
            }
            else
            {
                panel2.BackColor = Color.RoyalBlue;
            }
        }
        void btnClose_Click(object sender, EventArgs e)
        {
            pnlForm.Dispose();
            this.Dispose();
        }
        public Panel PanelForm
        {
            get { return pnlForm; }
        }
        public void Initialize(string Title)
        {
            this.Select();
            lblText.Text = Title;
        }
    }
}
